import RESSpriteFrame from "./RESSpriteFrame";
import TurnTable from "./TurnTable";
import { LanguageManager } from "./language/LanguageManager";
import Anim from "./utils/Anim";
import EffectUtils from "./utils/EffectUtils";
import MoneyChange from "./utils/MoneyChange";
import Utils from "./utils/Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameUI extends cc.Component {
    @property(cc.Node)
    private fingerNode: cc.Node = null;
    @property(cc.Label)
    private vertMoney: cc.Label = null;
    @property(cc.Node)
    private verticalBg: cc.Node = null
    @property(cc.Node)
    private horBg: cc.Node = null;
    @property(cc.Node)
    private table: cc.Node = null;
    @property(cc.Node)
    private tipNode: cc.Node = null;
    @property(cc.Label)
    private tipLabel: cc.Label = null;
    @property(cc.Prefab)
    private coinPrefab: cc.Prefab = null
    @property(cc.Node)
    private resultNode: cc.Node = null;
    private turnTable: TurnTable = null;
    private isFirst: boolean = true
    private canClick: boolean = true
    private moneyChange: MoneyChange = null
    protected start(): void {
        PlayerAdSdk.init();
        this.resize()
        let that = this;
        /**屏幕旋转尺寸改变 */
        cc.view.setResizeCallback(() => {
            that.resize();
        })
        this.tipLabel.string = LanguageManager.instance.getText(10007)
        this.turnTable = this.table.getComponent(TurnTable)
        this.node.on(cc.Node.EventType.TOUCH_START, this.startTable, this)
        let unit = LanguageManager.instance.getText(10001)
        this.moneyChange = new MoneyChange(this.vertMoney, false)
        this.moneyChange.prefix = unit
        this.vertMoney.string = `${unit}0`
    }
    private startTable() {
        this.canClick && cc.audioEngine.play(RESSpriteFrame.instance.clickAudioClip, false, 1)
        if (this.canClick) {
            this.fingerNode.active = this.tipNode.active = false
            let pos = Utils.getLocalPositionWithOtherNode(this.node, this.vertMoney.node)
            let target = this.vertMoney.node
            if (this.isFirst) {
                this.canClick = false
                setTimeout(() => {
                    cc.audioEngine.play(RESSpriteFrame.instance.turntable, false, 1)
                }, 300)
                this.turnTable.turnTo(11, () => {
                    cc.audioEngine.pauseAll()
                    let firstMoney = LanguageManager.instance.formatUnit(20)
                    Anim.ins().ShowFlyAni(this.coinPrefab, this.node, 20, pos, () => {
                        cc.audioEngine.pauseAll()
                        EffectUtils.removeTweens(target)
                        this.canClick = true
                        console.log('animation end');
                    }, this)
                    this.moneyChange.play(firstMoney, 1.5, () => {
                        setTimeout(() => {
                            this.tipLabel.string = LanguageManager.instance.getText(10008)
                            this.fingerNode.active = this.tipNode.active = true
                        }, 500)
                    })
                    cc.audioEngine.play(RESSpriteFrame.instance.numberAddAudioClip, false, 1)
                    cc.audioEngine.play(RESSpriteFrame.instance.coinAddAudioClip, false, 1)
                    console.log('first end');
                })
                this.isFirst = false
            } else {
                setTimeout(() => {
                    cc.audioEngine.play(RESSpriteFrame.instance.turntable, false, 1)
                }, 300)
                this.turnTable.turnTo(3, () => {
                    this.fingerNode.active = this.tipNode.active = false
                    console.log('last end');
                    let lastMoney = LanguageManager.instance.formatUnit(520)
                    Anim.ins().ShowFlyAni(this.coinPrefab, this.node, 30, pos, () => {
                        cc.audioEngine.pauseAll()
                        EffectUtils.removeTweens(target)
                        this.canClick = false
                        console.log('animation end');
                        setTimeout(() => {
                            this.resultNode.active = true
                            cc.audioEngine.play(RESSpriteFrame.instance.winAudioClip, false, 1)
                        }, 500)
                    }, this)
                    this.moneyChange.play(lastMoney, 2)
                    cc.audioEngine.play(RESSpriteFrame.instance.numberAddAudioClip, false, 1)
                    cc.audioEngine.play(RESSpriteFrame.instance.coinAddAudioClip, false, 1)
                    return
                })
            }
        }
    }
    private setText() {

    }
    private resize() {
        const canvasValue: any = cc.Canvas.instance;
        let frameSize = cc.view.getFrameSize();
        let isVertical = cc.winSize.height > cc.winSize.width
        this.fingerNode.angle = isVertical ? 15 : 0
        this.fingerNode.position = isVertical ? cc.v3(285, -300) : cc.v3(174, -355)
        if (isVertical) {//竖屏
            if (cc.winSize.width / cc.winSize.height > 0.7) {
                cc.Canvas.instance.fitHeight = true;
                cc.Canvas.instance.fitWidth = false;
            } else {
                cc.Canvas.instance.fitHeight = false;
                cc.Canvas.instance.fitWidth = true;
            }
        } else {
            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = false;
        }

        cc.director.getScene().getComponentsInChildren(cc.Widget).forEach(function (t) {
            t.updateAlignment()
        });
    }
    private onclickCashOut() {
        PlayerAdSdk.gameEnd()
        PlayerAdSdk.jumpStore()
        console.log('finished');

    }
}
